package org.gl.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.gl.logic.GLActions;
import org.gl.logic.Rule01Logic;

public class Rule01Window extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private GLWindow parent;
	
	private JLabel lStep, lDiff, lConstr;
	private JTextField tfStep, tfDiff;
	private JCheckBox cbConstr;
	private JButton bOk;
	
	public Rule01Window(GLWindow parent) {
		super("Rule 01 parameters");
		
		this.parent = parent;
		
		initView();
		
		pack();
		setVisible(true);
	}
	
	private void initView() {
		
		lStep = new JLabel("Step:");
		lDiff = new JLabel("Difference:");
		lConstr = new JLabel("Contraint:");
		
		tfStep = new JTextField();
		tfDiff = new JTextField();
		
		cbConstr = new JCheckBox();
		
		JPanel fldPane = new JPanel(new GridLayout(3, 2));
		fldPane.add(lStep);
		fldPane.add(tfStep);
		fldPane.add(lDiff);
		fldPane.add(tfDiff);
		fldPane.add(lConstr);
		fldPane.add(cbConstr);
		
		bOk = new JButton("OK");
		bOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int s = 0;
				int d = 0;
				boolean c = cbConstr.isSelected();
				try {
					s = Integer.parseInt(tfStep.getText());
					d = Integer.parseInt(tfDiff.getText());
				} catch(NumberFormatException ex) {
					JOptionPane.showMessageDialog(Rule01Window.this,
						    "Cannot initialize all variables.",
						    "Number format error",
						    JOptionPane.ERROR_MESSAGE);
					return;
				}
				parent.setGml(new Rule01Logic(s, d, c));
				parent.actionPerformed(new ActionEvent(this, GLActions.NEW_RULE, ""));
				
				setVisible(false);
			}
		});
		
		JPanel bttPane = new JPanel();
		bttPane.add(bOk);
		
		JPanel pane = new JPanel(new BorderLayout());
		pane.add(fldPane, BorderLayout.CENTER);
		pane.add(bttPane, BorderLayout.SOUTH);
		
		add(pane);
	}
}
